/* 
 * File:   pcf8563.h
 * Author: adamcsornyei
 *
 * Created on February 27, 2011, 6:32 PM
 */

#ifndef PCF8563_H
#define	PCF8563_H

#define RTC_CONTROL_STATUS1 0x00
#define RTC_CONTROL_STATUS2 0x01
#define RTC_SECONDS         0x02
#define RTC_MINUTES         0x03
#define RTC_HOURS           0x04
#define RTC_DAYS            0x05
#define RTC_WEEKDAYS        0x06
#define RTC_MONTHS          0x07
#define RTC_YEARS           0x08
#define RTC_MINUTE_ALARM    0x09
#define RTC_HOURS_ALARM     0x0A
#define RTC_DAY_ALARM       0x0B
#define RTC_WEEKDAY_ALARM   0x0C
#define RTC_CLKOUT_CONTROL  0x0D
#define RTC_TIMER_CONTROL   0x0E
#define RTC_TIMER           0x0F

//CONTROL_STATUS2
#define RTC_TITP            4
#define RTC_AF              3
#define RTC_TF              2
#define RTC_AIE             1
#define RTC_TIE             0

//SECONDS
#define RTC_VL              7

//WEEKDAYS
#define RTC_MON             0x01
#define RTC_TUE             0x02
#define RTC_WED             0x03
#define RTC_THU             0x04
#define RTC_FRI             0x05
#define RTC_SAT             0x06
#define RTC_SUN             0x00

//MONTHS
#define RTC_CEN             7

#define RTC_JAN             0x01
#define RTC_FEB             0x02
#define RTC_MAR             0x03
#define RTC_APR             0x04
#define RTC_MAY             0x05
#define RTC_JUN             0x06
#define RTC_JUL             0x07
#define RTC_AUG             0x08
#define RTC_SEP             0x09
#define RTC_OCT             0x10
#define RTC_NOV             0x11
#define RTC_DEC             0x12

//CLKOUT_CONTROL
#define RTC_FE              7
#define RTC_FD0             0
#define RTC_FD1             1

//ALARM
#define RTC_AE              7

//TIMER_CONTROL
#define RTC_TE              7
#define RTC_TD0             0
#define RTC_TD1             1

#define RTC_ADDR 0xA2

typedef struct{//bcd
    uint8_t sec;//VL
    uint8_t min;
    uint8_t hour;
    uint8_t day;
    uint8_t wday;
    uint8_t mon;
    uint8_t year;
}RTCtime;

typedef struct{//bcd
    uint8_t min;
    uint8_t hour;
    uint8_t day;
    uint8_t wday;
}RTCalarm;

typedef struct{
    uint8_t con1;
    uint8_t con2;
    uint8_t timer;
    uint8_t clkout;
}RTCcontrol;

typedef struct{
    RTCtime     time;
    RTCalarm    alarm;
    RTCcontrol  control;
    uint8_t timer;
}RTC;


void rtcInit(void);
void rtcReadClock(uint8_t *p);
void rtcSetClock();
void rtcSetTimer(void);
void rtcSetControl(void);
uint8_t rtc8Bit2BCD2(uint8_t);
uint8_t rtc2BCD2Bit8(uint8_t);

#endif	/* PCF8563_H */

